package com.itheima.controller;


import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UpLoadController {


    @Autowired
    private AliOSSUtils aliOSSUtils;
//    本地存储文件

//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile image) throws Exception {
//        log.info("文件上传：{}，{}，{}",username,age,image);
//
//
////        uuid（通用唯一识别码）
//
//
//
//        String originalFilename = image.getOriginalFilename();
//
//        //获取文件扩展名
//        int index = originalFilename.lastIndexOf(".");
//        String extname = originalFilename.substring(index);
//        String newfilename = UUID.randomUUID().toString()+extname;
//        log.info("新文件名：{}",newfilename);
//
//        image.transferTo(new File("H:\\imgs\\"+newfilename));
//
//
//        return Result.success();
//    }


    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {

        log.info("文件上传：{}", image.getOriginalFilename());
        //调用阿里云oss工具类
        String url = aliOSSUtils.upload(image);
        log.info("文件上传成功，文件访问路径：{}", url);
        return Result.success(url);


    }
}



